﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_ucOrder : System.Web.UI.UserControl
{
    TblOrderDAO orderDAO = new TblOrderDAO(Global.CONNECTION_STRING);
    DateTime _dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // hien thi ngay dau tien - ngay cuoi cung trong thang
            _dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime date = _dt;
        
            txtFrDate.Text = _dt.ToString("dd/MM/yyyy");
           // txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            int ngay = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            int thang = DateTime.Now.Month;
            int nam = DateTime.Now.Year;
            txtToDate.Text = Convert.ToDateTime(nam + "/" + thang + "/" + ngay).ToString("dd/MM/yyyy");
            loaddata();
        }
        if (Request.Params["__EVENTTARGET"] != null)
        {
            if (Request.Params["__EVENTTARGET"].IndexOf("DataPagerImages") > 0)
            {
                loaddata();
            }
        }
    }
    private void loaddata()
    {
        try 
        {
            TblOrderDAO orderDAO = new TblOrderDAO(Global.CONNECTION_STRING);
            DataTable dt = orderDAO.GetOrderList(Global.ConvertToDate(txtFrDate.Text), Global.ConvertToDate(txtToDate.Text), "", int.Parse(DropDownList1.SelectedValue.ToString()));
            lvItem.DataSource = dt;
            lvItem.DataBind();
            
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }

    protected void lvItem_PagePropertiesChanged(object sender, EventArgs e)
    {
        lvItem.DataBind();
    }
    protected void btnSubmit_Click1(object sender, ImageClickEventArgs e)
    {
        loaddata();
    }

    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 0: dat hang, 1: da giao hang , 2: huy don hang, 3: Da chuyen tien
        
        string ItemID = "";
        foreach (ListViewItem lst in lvItem.Items)
        {
            DropDownList drStatus = (DropDownList)lst.FindControl("drStatus");
            HiddenField hid = (HiddenField)lst.FindControl("hid");
            ItemID = hid.Value;
            DataTable dt = null;

            if (drStatus.SelectedValue == "0")
            {
                dt = orderDAO.Update(0, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "1")
            {
                dt = orderDAO.Update(1, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "2")
            {
                dt = orderDAO.Update(2, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "3")
            {
                dt = orderDAO.Update(3, Global.ConvertToInt(ItemID));
            }
        }

      
    }
    protected void lvItem_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            string hid = DataBinder.Eval(dataItem.DataItem, "OrderID").ToString();
            HiddenField hid1 = (HiddenField)e.Item.FindControl("hid");
            hid1.Value = hid;

            DataTable Order = new DataTable();
            TblOrderDAO orderDAO = new TblOrderDAO(Global.CONNECTION_STRING);
            Order = orderDAO.GetOrder(Convert.ToInt32(hid));
            DropDownList drStatus = (DropDownList)e.Item.FindControl("drStatus");


            foreach (DataRow dr in Order.Rows)
            {
                if (dr["Status"].ToString() =="0")
                {
                    drStatus.SelectedItem.Text = "Đặt hàng";
                   
                }
                if (dr["Status"].ToString() == "1")
                {
                    drStatus.SelectedItem.Text = "Đã giao hang";
                   
                }
                if (dr["Status"].ToString() == "2")
                {
                    drStatus.SelectedItem.Text = "Huỷ đơn hàng";
                }
                if (dr["Status"].ToString() == "3")
                {
                    drStatus.SelectedItem.Text = "Đã chuyển tiền";
                }
            }
        }
    }
}
